<?php

/**
 * When a page is called, the page controller is run before any output is made. 
 * The controller's job is to transform the HTTP request into business objects, 
 * then call the approriate logic and prepare the objects used to display the response.
 * 
 * The page logic performs the following steps:
 * 1. The cmd request parameter is evaluated.
 * 2. Based on the action other request parameters are evaluated.
 * 3. Value Objects (or a form object for more complex tasks) are created from the parameters.
 * 4. The objects are validated and the result is stored in an error array.
 * 5. The business logic is called with the Value Objects.
 * 6. Return status (error codes) from the business logic is evaluated.
 * 7. A redirect to another page is executed if necessary.
 * 8. All data needed to display the page is collected and made available to the page as variables of the controller. Do not use global variables.
 *
 * * @author dintech
 * 
 */

require_once('include/header.php');
require_once('vo/UsuarioVO.php');
require_once('search/UsuarioSearchCriteria.php');
require_once('logic/UsuariosLogic.php');
require('conf/configuration.php');


$logic = new UsuariosLogic($_POST);

$id = $_GET['id'];
if ($id != null){
	$usuario = $logic->get($id);
	$smarty->assign('usuario', $usuario);
}

$action = $_GET['action'];
if ($action == null){
	$action = $_POST['action'];
}

switch ($action){
	case "add":
		if ($_POST['sendform']!="1"){
			$smarty->assign('detailPage', 'usuariosAdd.tpl');
			$smarty->display('admin_generic.tpl');
			exit;
		}
		else{
			$result = $logic->save($_POST);
			if ($result == 'error')
				$smarty->assign('error', "Ha ocurrido un error al crear el usuario");
			else
				$smarty->assign('message', "El usuario <b>" . $_POST['username'] . "</b> ha sido creado");
		}
		break;
	case "update":
		if ($_POST['sendform']!="1"){
			$smarty->assign('detailPage', 'usuariosUpdate.tpl');
			$smarty->display('admin_generic.tpl');
			
			exit;
		}
		else{
			$result = $logic->save($_POST);
			if ($result == 'error')
				$smarty->assign('error', "Ha ocurrido un error al guardar los cambios del usuario");
			else
				$smarty->assign('message', "El usuario <b>" . $_POST['username'] . "</b> ha sido actualizado");
		}
		break;
	case "delete":
		$result = $logic->delete($_POST['id']);
		if ($result == 'error')
			$smarty->assign('error', "Ha ocurrido un error al eliminar el usuario");
		else
			$smarty->assign('message', "Se ha eliminado el usuario");
		break;
}

$usuariosearchCriteria = new UsuarioSearchCriteria();
$usuariosearchCriteria->setOrder("DESC");
$usuariosearchCriteria->setInicio(1);
$usuariosearchCriteria->setFin(null);
$cantidad = $logic->count($usuariosearchCriteria);


// Paginador
$totalPages = ceil($cantidad / $totalItemsPerPage);
$smarty->assign('totalPages', $totalPages);
$smarty->assign('totalItemsPerPage', $totalItemsPerPage);

$page = $_GET[page];
if (!$page) {
	$start = 0;
	$page = 1;
	$smarty->assign('page', $page);
}
else
	$start = ($page - 1) * $totalItemsPerPage;
$smarty->assign('page', $page);
	
$usuariosearchCriteria->setInicio($start);	
$usuariosearchCriteria->setFin($totalItemsPerPage);
$usuarios = $logic->find($usuariosearchCriteria);
	
$smarty->assign('cantidad', $cantidad);
$smarty->assign('usuarios', $usuarios);
$smarty->assign('detailPage', 'usuarios.tpl');
$smarty->display('admin_generic.tpl');

?>